<template>
  <div class="article-container">
    <el-card>
      <template #header>
        <div class="header">
          <div class="search-area">
            <el-input
              v-model="searchQuery"
              placeholder="请输入文章标题搜索"
              style="width: 200px"
              clearable
              @clear="handleSearch"
            />
            <el-select v-model="categoryId" placeholder="选择分类" clearable style="margin-left: 10px">
              <el-option label="全部分类" value="" />
              <el-option label="技术" value="1" />
              <el-option label="生活" value="2" />
            </el-select>
          </div>
          <el-button type="primary" @click="handleAdd">
            <el-icon><Plus /></el-icon>
            写文章
          </el-button>
        </div>
      </template>

      <el-table :data="tableData" style="width: 100%" v-loading="loading">
        <el-table-column prop="title" label="标题" show-overflow-tooltip />
        <el-table-column prop="categoryName" label="分类" width="100" />
        <el-table-column prop="tags" label="标签" width="200">
          <template #default="{ row }">
            <el-tag
              v-for="tag in row.tags"
              :key="tag"
              size="small"
              style="margin-right: 5px"
            >{{ tag }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="author" label="作者" width="100" />
        <el-table-column prop="viewCount" label="浏览量" width="100" align="center" />
        <el-table-column prop="status" label="状态" width="100">
          <template #default="{ row }">
            <el-tag :type="row.status === 1 ? 'success' : row.status === 0 ? 'info' : 'warning'">
              {{ row.status === 1 ? '已发布' : row.status === 0 ? '草稿' : '待审核' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="180" />
        <el-table-column label="操作" width="200" fixed="right">
          <template #default="{ row }">
            <el-button link type="primary" @click="handleEdit(row)">编辑</el-button>
            <el-button link type="primary" @click="handlePreview(row)">预览</el-button>
            <el-button link type="danger" @click="handleDelete(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 30, 50]"
          layout="total, sizes, prev, pager, next"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { Plus } from '@element-plus/icons-vue'
import { ElMessage, ElMessageBox } from 'element-plus'

const searchQuery = ref('')
const categoryId = ref('')
const loading = ref(false)
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)

const tableData = ref([
  {
    id: 1,
    title: 'Vue3 组合式 API 最佳实践',
    categoryName: '技术',
    tags: ['Vue3', 'JavaScript'],
    author: '张三',
    viewCount: 1000,
    status: 1,
    createTime: '2025-07-18 10:00:00'
  },
  {
    id: 2,
    title: '2025年前端技术趋势展望',
    categoryName: '技术',
    tags: ['前端', 'Web开发'],
    author: '李四',
    viewCount: 850,
    status: 0,
    createTime: '2025-07-17 14:30:00'
  }
])

const handleSearch = () => {
  // 实现搜索逻辑
}

const handleAdd = () => {
  // 实现添加文章逻辑
}

const handleEdit = (row) => {
  // 实现编辑文章逻辑
}

const handlePreview = (row) => {
  // 实现预览文章逻辑
}

const handleDelete = (row) => {
  ElMessageBox.confirm('确认删除该文章?', '警告', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    // 实现删除文章逻辑
    ElMessage.success('删除成功')
  }).catch(() => {})
}

const handleSizeChange = (val) => {
  pageSize.value = val
  // 重新加载数据
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  // 重新加载数据
}
</script>

<style scoped>
.article-container {
  padding: 20px;
}
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.search-area {
  display: flex;
  align-items: center;
}
.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>
